package com.apexsoft.assets.base.aspect;


import com.apexsoft.assets.base.anotation.AccessValidate;
import com.apexsoft.assets.base.utils.AccessPermissionUtil;
import com.apexsoft.live.exception.AuthException;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Order(1)
public class PermissionValidateAspect {
    @Value("${application.access.enabled:false}")
    private boolean accessControlled;

    private static final Logger logger = LoggerFactory.getLogger(PermissionValidateAspect.class);

    @Pointcut("@annotation(com.apexsoft.assets.base.anotation.AccessValidate)")
    public void permission() {
    }

    @Before("permission()&&@annotation(accessValidate)")
    public void before(JoinPoint joinPoint, AccessValidate accessValidate) throws Throwable {
        if(accessControlled && !AccessPermissionUtil.hasAccess(accessValidate.accessName().getAccessCode())){
            throw new AuthException(-99, "无访问权限");
        }
    }
}
